package pers.lipeng;

import pers.lipeng.gobang.GoBangGame;
import pers.lipeng.snake.HistoryScoreList;
import pers.lipeng.snake.SnakeGame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.im.InputContext;
import java.util.Locale;

/**
 * @author APengG
 */
public class GreedySnakeApplication extends JFrame {
    private volatile int snakeLevel = 1;
    private CardLayout cardLayout;
    private JPanel appJPanel;
    public GreedySnakeApplication(){
        super("垃圾游戏平台");
        this.setSize(600,500);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = screenSize.width - this.getWidth() >> 1;
        int y = screenSize.height - this.getHeight() >> 1;
        this.setLocation(x,y);
        JMenuBar jMenuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenuItem exitMenuItem = new JMenuItem("Exit");
        exitMenuItem.addActionListener(evet->{
            System.exit(0);
        });
        fileMenu.add(exitMenuItem);
        JMenu gameMenu = new JMenu("Game");
        JMenuItem snake = new JMenuItem("贪吃蛇");
        snake.setActionCommand("snake");
        snake.addActionListener(new GameMenuActionListener());
        JMenuItem gobang = new JMenuItem("五子棋");
        gobang.setActionCommand("gobang");
        gobang.addActionListener(new GameMenuActionListener());
        gameMenu.add(snake);
        gameMenu.add(gobang);
        JMenu helpMenu = new JMenu("Help");


        jMenuBar.add(fileMenu);
        jMenuBar.add(gameMenu);
        jMenuBar.add(helpMenu);
        this.setJMenuBar(jMenuBar);

        //卡片布局
        appJPanel = new JPanel();
        cardLayout = new CardLayout();
        appJPanel.setLayout(cardLayout);
        //贪吃蛇
        JSplitPane jSplitPane = initSnakeGame();
        //五子棋
        JPanel jPanel = initGoBangGame();
        appJPanel.add("snake",jSplitPane);
        appJPanel.add("gobang",jPanel);
        this.getContentPane().add(appJPanel);


        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        InputContext instance = InputContext.getInstance();
        instance.selectInputMethod(Locale.ENGLISH);
    }

    private JSplitPane initSnakeGame(){
        //游戏等级单选框
        ButtonGroup buttonGroup = new ButtonGroup();
        JRadioButton jRadioButton1 = new JRadioButton("简单",true);
        jRadioButton1.setActionCommand("1");
        jRadioButton1.addActionListener(new SnakeLevelActionListener());
        JRadioButton jRadioButton2 = new JRadioButton("中等",false);
        jRadioButton2.setActionCommand("2");
        jRadioButton2.addActionListener(new SnakeLevelActionListener());
        JRadioButton jRadioButton3 = new JRadioButton("困难",false);
        jRadioButton3.setActionCommand("3");
        jRadioButton3.addActionListener(new SnakeLevelActionListener());
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);



        JButton jButton = new JButton("开始游戏");
        jButton.addActionListener((event)->{
            SnakeGame snakeGame = new SnakeGame();
            snakeGame.setLevel(snakeLevel);
        });

        JPanel radioJPanel = new JPanel();
        radioJPanel.add(jRadioButton1);
        radioJPanel.add(jRadioButton2);
        radioJPanel.add(jRadioButton3);


        JLabel snakeJLabel = new JLabel();
        ImageIcon snakeImageIcon = new ImageIcon(getClass().getResource("/snake/img/snake.png"));
        snakeJLabel.setIcon(snakeImageIcon);
        snakeJLabel.setSize(new Dimension(600,350));
        Image scaledInstance = snakeImageIcon.getImage().getScaledInstance(
                snakeJLabel.getWidth(),
                snakeJLabel.getHeight(),
                Image.SCALE_DEFAULT);
        snakeImageIcon.setImage(scaledInstance);

        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout());
        jPanel.add(snakeJLabel,BorderLayout.NORTH);
        jPanel.add(radioJPanel,BorderLayout.CENTER);
        jPanel.add(jButton,BorderLayout.SOUTH);

        JSplitPane jSplitPane = new JSplitPane();
        jSplitPane.setLeftComponent(HistoryScoreList.getInstance());
        jSplitPane.setRightComponent(jPanel);

        return jSplitPane;
    }

    private JPanel initGoBangGame(){
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout());
        JLabel jLabel = new JLabel();
        ImageIcon imageIcon = new ImageIcon(getClass().getResource("/gobang/img/gobang.png"));
        jLabel.setIcon(imageIcon);
        jPanel.add(jLabel,BorderLayout.NORTH);

        JButton jButton = new JButton("开始游戏");
        jPanel.add(jButton,BorderLayout.SOUTH);
        jButton.addActionListener((event)->{
            GoBangGame goBangGame = new GoBangGame();
        });
        Image scaledInstance =
                imageIcon.getImage().getScaledInstance(
                        600,
                        400,
                        Image.SCALE_DEFAULT);
        imageIcon.setImage(scaledInstance);
        jLabel.setIcon(imageIcon);
        jPanel.revalidate();
        return jPanel;
    }

    class GameMenuActionListener implements  ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            String actionCommand = e.getActionCommand();
            if("snake".equals(actionCommand)){
                cardLayout.show(appJPanel,"snake");
            }else if("gobang".equals(actionCommand)){
               cardLayout.show(appJPanel,"gobang");
            }
        }
    }
    class SnakeLevelActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            String actionCommand = e.getActionCommand();
            snakeLevel = Integer.valueOf(actionCommand);
        }
    }
    public static void main(String[] args) {
        new GreedySnakeApplication();
    }

}
